<?php
namespace app\admin\controller\goods;
use app\admin\controller\Controller;
use app\admin\service\Goods as GoodsModel;
use think\Db;
class Goods extends Controller{
    /**
     * 首页数据列表
     */
    public function index(){
        // 获取全部数据列表
        $type=input('get.type');
        $title=input('get.name');
        if($title){
            $where['title|name|content']=["like",'%'.trim($title).'%'];
        }
        if($type){
            $where['type']=["=",$type];
        }
        if($type||$title){
            $list = Db::name('love_notice')->where($where)->order('create_time desc')->paginate(15,false, [
                'query' => \request()->request()
            ]);
        }else{
            $list = Db::name('love_notice')->order('create_time desc')->paginate(15,false, [
                'query' => \request()->request()
            ]);
        }
        return $this->fetch('index', compact('list'));
    }
    /**
     * 添加首页数据
     */
    public function add(){
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        $data=$this->postData('article');
        $data['create_time']=time();
        if($data['type']==1){
            $names=Db::name('love_notice')->where(['type'=>$data['type']])->find();
            if($names){
                return $this->renderError('logo数据只能添加一条');
            }
        }

        //新增记录
        if (Db::name('love_notice')->insertGetId($data)) {
            return $this->renderSuccess('添加成功', url('goods.goods/index'));
        }
        return $this->renderError('添加失败');
    }

    /**
     * 更新首页数据
     */
    public function edit($id){
        //数据详情
        $model = Db::name('love_notice')->where(['id'=>$id])->find();
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        // 更新记录
        $data=$this->postData('article');
        $data['update_time']=time();
        if (Db::name('love_notice')->where(['id'=>$id])->update($data)) {
            return $this->renderSuccess('更新成功', url('goods.goods/index'));
        }
        return $this->renderError( '更新失败');
    }
    /**
     * 删除数据
     */
    public function delete($id){
        $del=Db::name('love_notice')->where(['id'=>$id])->update(['status'=>2]);
        if($del){
            return $this->renderSuccess('删除成功', url('goods.goods/index'));
        }
        return $this->renderError( '删除失败');
    }
    /**
     * 恢复数据
     */
    public function recover($id){
        $del=Db::name('love_notice')->where(['id'=>$id])->update(['status'=>1]);
        if($del){
            return $this->renderSuccess('恢复成功', url('goods.goods/index'));
        }
        return $this->renderError( '恢复失败');
    }

}